package com.dz.risk.controller;

import com.dz.risk.common.ResponseVO;
import com.dz.risk.common.utils.ResponseUtils;
import com.dz.risk.custom.riskFactorBacktracking.RiskFactorBacktrackingExport;
import com.dz.risk.pojo.domain.RiskFactorBacktracking;
import com.dz.risk.service.RiskFactorBacktrackingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @author Doke
 * @date 2021/7/1 14:29
 */
@RestController
@RequestMapping("/dz-ftsp/stest/riskFactorBacktracking")
@Api(tags = "风险因子回溯Api")
public class RiskFactorBacktrackingController {

    private final RiskFactorBacktrackingService riskFactorBacktrackingService;

    private final RiskFactorBacktrackingExport riskFactorBacktrackingExport;

    @Autowired
    public RiskFactorBacktrackingController(RiskFactorBacktrackingService riskFactorBacktrackingService, RiskFactorBacktrackingExport riskFactorBacktrackingExport) {
        this.riskFactorBacktrackingService = riskFactorBacktrackingService;
        this.riskFactorBacktrackingExport = riskFactorBacktrackingExport;
    }

    @ApiOperation("根据版本查询风险因子回溯信息")
    @ApiImplicitParam(name = "version", value = "日期版本 例:2020-12-11", required = true, dataType = "date")
    @PostMapping("/selectRiskFactorBacktrackingByVersion")
    public ResponseVO selectRiskFactorBacktrackingByVersion(@RequestBody RiskFactorBacktracking riskFactorBacktracking) {
        if (StringUtils.isBlank(riskFactorBacktracking.getVersion())) {
            return ResponseVO.fail("请先选择时间");
        }
        PageHelper.startPage(riskFactorBacktracking.getPageNum(), riskFactorBacktracking.getPageSize());
        return new ResponseVO(new PageInfo<>(riskFactorBacktrackingService.selectByParams(riskFactorBacktracking)));
    }

    @ApiOperation("根据版本导出风险因子回溯详情")
    @ApiImplicitParam(name = "version", value = "日期版本 例:2020-12-11", required = true, dataType = "date")
    @GetMapping("/doExportByVersion")
    public void doExportByVersion(HttpServletResponse response, @RequestParam("version") String version) throws IOException {
        final String fileName = "风险因子回溯详情_%s.xlsx";
        ServletOutputStream outputStream = ResponseUtils.getOutputStreamAndSettingFileFormat(response, fileName);
        riskFactorBacktrackingExport.doExportByVersion(outputStream, version);
    }


    @ApiOperation("计算风险因子回溯")
    @ApiImplicitParam(name = "version", value = "日期版本 例:2020-12-11", required = true, dataType = "date")
    @PostMapping("/calculateRiskFactorBacktrackingByVersion")
    public ResponseVO calculateRiskFactorBacktrackingByVersion(@RequestBody RiskFactorBacktracking riskFactorBacktracking) {
        if (StringUtils.isBlank(riskFactorBacktracking.getVersion())) {
            return ResponseVO.fail("请先选择时间");
        }
        riskFactorBacktrackingService.calculateRiskFactorBacktrackingByVersion(riskFactorBacktracking);
        return ResponseVO.ok();
    }
}
